package net.yrom.screenrecorder.ui.floating;

import android.content.Context;
import android.content.Intent;
import android.view.ContextThemeWrapper;
import android.view.View;

import com.makeramen.roundedimageview.RoundedImageView;
import com.stardust.enhancedfloaty.FloatyService;
import com.stardust.floatingcircularactionmenu.CircularActionMenu;

import net.yrom.screenrecorder.R;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;

import static net.yrom.screenrecorder.MainActivity.ACTION_START;
import static net.yrom.screenrecorder.MainActivity.ACTION_STOP;

/**
 * Created by Stardust on 2017/10/18.
 */

public class CircularMenu {


    public static class StateChangeEvent {
        private int currentState;
        private int previousState;

        public StateChangeEvent(int currentState, int previousState) {
            this.currentState = currentState;
            this.previousState = previousState;
        }

        public int getCurrentState() {
            return currentState;
        }

        public int getPreviousState() {
            return previousState;
        }
    }

    public static final int STATE_CLOSED = -1;
    public static final int STATE_NORMAL = 0;
    public static final int STATE_RECORDING = 1;

    private static final int IC_ACTION_VIEW = R.drawable.ic_android_eat_js;

    CircularMenuWindow mWindow;
    private int mState;
    private RoundedImageView mActionViewIcon;
    private Context mContext;
    //    private MaterialDialog mSettingsDialog;
//    private MaterialDialog mLayoutInspectDialog;
    private String mRunningPackage, mRunningActivity;

    public CircularMenu(Context context) {
        mContext = new ContextThemeWrapper(context, R.style.AppTheme);
        initFloaty();
        setupListeners();
//        mRecorder = GlobalActionRecorder.getSingleton(context);
//        mRecorder.addOnStateChangedListener(this);
//        AutoJs.getInstance().getLayoutInspector().addCaptureAvailableListener(this);
    }

    private void setupListeners() {
        mWindow.setOnActionViewClickListener(v -> {
            if (mState == STATE_RECORDING) {
                stopRecord();
            } else if (mWindow.isExpanded()) {
                mWindow.collapse();
            } else {
//                mCaptureDeferred = new DeferredObject<>();
//                AutoJs.getInstance().getLayoutInspector().captureCurrentWindow();
                mWindow.expand();
            }
        });
    }

    private void initFloaty() {
        mWindow = new CircularMenuWindow(mContext, new CircularMenuFloaty() {

            @Override
            public View inflateActionView(FloatyService service, CircularMenuWindow window) {
                View actionView = View.inflate(service, R.layout.circular_action_view, null);
                mActionViewIcon = (RoundedImageView) actionView.findViewById(R.id.icon);
                return actionView;
            }

            @Override
            public CircularActionMenu inflateMenuItems(FloatyService service, CircularMenuWindow window) {
                CircularActionMenu menu = (CircularActionMenu) View.inflate(new ContextThemeWrapper(service, R.style.AppTheme), R.layout.circular_action_menu, null);
                ButterKnife.bind(CircularMenu.this, menu);
                return menu;
            }
        });
        mWindow.setKeepToSideHiddenWidthRadio(0.25f);
        FloatyService.addWindow(mWindow);
    }


    @Optional
    @OnClick(R.id.script_list)
    void showScriptList() {
        mWindow.collapse();
    }

    @Optional
    @OnClick(R.id.record)
    void startRecord() {
        mWindow.collapse();
        Intent intent = new Intent(ACTION_START).setPackage(mContext.getPackageName());
        mContext.sendBroadcast(intent);
    }


    private void stopRecord() {
    }

    @Optional
    @OnClick(R.id.layout_inspect)
    void inspectLayout() {
        mWindow.collapse();

    }


    @Optional
    @OnClick(R.id.stop_all_scripts)
    void stopAllScripts() {
        mWindow.collapse();
        Intent intent = new Intent(ACTION_STOP).setPackage(mContext.getPackageName());
        mContext.sendBroadcast(intent);
    }


    @Optional
    @OnClick(R.id.settings)
    void settings() {

    }


    void enableAccessibilityService() {
        dismissSettingsDialog();
    }


    private void dismissSettingsDialog() {

    }


    @Optional
    void openLauncher() {
        dismissSettingsDialog();

    }

    public void close() {
        dismissSettingsDialog();
        try {
            mWindow.close();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            mState = STATE_CLOSED;
        }

    }

}
